'use strict';
module.exports = function (sequelize, DataTypes) {

  var Team = sequelize.define('Team', {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    organizationId: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      validate: {
        isInt: true
      },
      comment: '组织ID'
    },
    roleId: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      validate: {
        isInt: true
      },
      comment: '组织ID'
    },
    teamName: {
      type: DataTypes.STRING(41),
      allowNull: false,
      comment: '分组名称'
    }
  }, {
    classMethods: {
      associate: function (models) {
        Team.belongsTo(models.Organization, {
          foreignKey: 'organizationId',
          constraints: false
        });
        Team.belongsTo(models.Role, {
          foreignKey: 'roleId',
          constraints: false
        });
        Team.belongsToMany(models.User, {
          through: models.TeamUser,
          foreignKey: 'teamId',
          constraints: false
        });
      }
    },
    timstamps: true,
    paranoid: true,
    tableName: 'teams'
  });

  return Team;
};
